package com.ciaojian.worker.controller;

import cn.hutool.core.date.DateUtil;
import com.ciaojian.core.base.annotation.ApiIdempotent;
import com.ciaojian.core.enums.sys.StatusCode;
import com.ciaojian.core.model.group.Save;
import com.ciaojian.core.model.vo.CheckUser;
import com.ciaojian.core.model.vo.ClockDayVo;
import com.ciaojian.core.model.vo.ClockMonthVo;
import com.ciaojian.core.model.vo.ClockStatisticsVo;
import com.ciaojian.core.util.Result;
import com.ciaojian.worker.model.param.CheckUserDTO;
import com.ciaojian.worker.service.CheckUserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Atlakyn
 */
@Api(tags = "打卡")
@RestController
@RequestMapping("/check-user")
public class CheckUserController {
    @Resource
    private CheckUserServiceImpl checkUserService;

    @ApiOperation(value = "考勤打卡",response = CheckUserDTO.class)
    @ApiIdempotent
    @PostMapping("/clock-in")
    public Result clockIn(@RequestBody @Validated(Save.class) CheckUserDTO param) throws ParseException {
        CheckUser checkUser = new CheckUser();
        param.setDate(DateUtil.today());
        param.setTime(new SimpleDateFormat("HH:mm:ss").format(new Date()));

        Date curDate = new SimpleDateFormat("HH:mm:ss").parse(param.getTime());
        checkUser.setCurDate(curDate);
        BeanUtils.copyProperties(param, checkUser);

        String punchIn = "上班打卡";
        String punchOff = "下班打卡";
        if (punchIn.equals(param.getType())) {
            return Result.save(checkUserService.punchIn(checkUser));
        } else if (punchOff.equals(param.getType())){
            return Result.save(checkUserService.punchOff(checkUser));
        }
        return Result.fail(StatusCode.FAIL);
    }


    /**
     * 员工考核日历
     * @param workerId
     * @param month
     * @return
     */
    @ApiIdempotent
    @GetMapping("/myClockMonth")
    public Result myClockMonth(@RequestParam int workerId,
                               @RequestParam String month) {
        List<ClockMonthVo> clockMonthVos = checkUserService.myClockMonth(workerId,month);
        return Result.ok(clockMonthVos);
    }


    /**
     * 员工考核日历
     * @param workerId
     * @param day
     * @return
     */
    @ApiIdempotent
    @GetMapping("/myClockDay")
    public Result myClockDay(@RequestParam int workerId,
                             @RequestParam String day) throws ParseException {
        ClockDayVo clockDay = checkUserService.myClockDay(workerId,day);
        return Result.ok(clockDay);
    }

    /**
     * 员工考核月统计
     * @param workerId
     * @param month
     * @return
     */
    @ApiIdempotent
    @GetMapping("/myClockStatistics")
    public Result myClockStatistics(@RequestParam int workerId,
                                  @RequestParam String month) {
        ClockStatisticsVo clockStatisticsVo = checkUserService.myClockStatistics(workerId,month);
        return Result.ok(clockStatisticsVo);
    }



}
